﻿using LeadTracker.DataAccess;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Granite.Data;
using LeadTracker.Models;

namespace DataAccess_Tests
{
	[TestClass()]
	public class UserRepositoryTest
	{

		UserRepository m_UserRepo;
		EditorRepository m_EditorRepo;
		SqlServerConnection m_Connection;

		[TestInitialize]
		public void Setup()
		{
			m_Connection = new SqlServerConnection("Data Source=localhost\\SQLEXPRESS;Integrated Security=True;Database=LeadTracker");
			m_UserRepo = new UserRepository(m_Connection);
			m_EditorRepo = new EditorRepository(m_Connection);
		}

		[TestMethod()]
		public void UpdatePasswordTest()
		{
			var newUser = new EditorModel();
			newUser.UserName = "TestUser" + DateTime.Now.Ticks;
			newUser.EmailAddress = newUser.UserName + "@fake";
			newUser.FirstName = "Test";
			newUser.LastName = "User";

			var random = new Random();
			var password = random.Next().ToString();

			m_UserRepo.CreateUser(newUser, password);

			var v1 = m_UserRepo.ValidateUser(newUser.UserName, password);
			Assert.IsTrue(v1);

			var newPassword = password + "X";

			var errorChanged = m_UserRepo.ChangePassword(newUser.UserName, password + "FAIL", newPassword);
			Assert.IsFalse(errorChanged);

			var changed = m_UserRepo.ChangePassword(newUser.UserName, password, newPassword);
			Assert.IsTrue(changed);

			var v2 = m_UserRepo.ValidateUser(newUser.UserName, password);
			var v3 = m_UserRepo.ValidateUser(newUser.UserName, newPassword);
			Assert.IsFalse(v2);
			Assert.IsTrue(v3);
		}

		[TestMethod()]
		public void CreateAndValidateUser()
		{

			var newUser = new EditorModel();
			newUser.UserName = "TestUser" + DateTime.Now.Ticks;
			newUser.EmailAddress = newUser.UserName + "@fake";
			newUser.FirstName = "Test";
			newUser.LastName = "User";
			Assert.AreEqual("Test User", newUser.FullName);
			Assert.IsTrue(newUser.IsDirty);

			var random = new Random();
			var password = random.Next().ToString();

			m_UserRepo.CreateUser(newUser, password);
			try
			{
				var badUser = new EditorModel();
				badUser.UserName = newUser.UserName;
				badUser.EmailAddress = Guid.NewGuid() + "@fake";
				badUser.FirstName = "Test";
				badUser.LastName = "User";

				m_UserRepo.CreateUser(badUser, password);
			}
			catch (CreateUserException ex)
			{
				Assert.AreEqual(CreateUserError.DuplicateUserName, ex.ErrorCode);
			}
			try
			{
				var badUser = new EditorModel();
				badUser.UserName = Guid.NewGuid().ToString();
				badUser.EmailAddress = newUser.EmailAddress;
				badUser.FirstName = "Test";
				badUser.LastName = "User";

				m_UserRepo.CreateUser(badUser, password);
			}
			catch (CreateUserException ex)
			{
				Assert.AreEqual(CreateUserError.DuplicateEmailAddress, ex.ErrorCode);
			}

			Assert.IsFalse(newUser.IsDirty);
			Assert.AreNotEqual(0, newUser.EditorKey);

			var validated = m_UserRepo.ValidateUser(newUser.UserName, password);
			Assert.IsTrue(validated);

			var notValidated = m_UserRepo.ValidateUser(newUser.UserName, password + "X");
			Assert.IsFalse(notValidated);

			var loadedUser = m_EditorRepo.SelectEditor (newUser.EditorKey, 0);

			Assert.IsFalse(loadedUser.HasErrors);
			Assert.IsFalse(loadedUser.IsDirty);
			Assert.AreEqual(newUser.EditorKey, loadedUser.EditorKey);
			Assert.AreEqual(newUser.EmailAddress, loadedUser.EmailAddress);
			Assert.AreEqual(newUser.FirstName, loadedUser.FirstName);
			Assert.AreEqual(newUser.FullName, loadedUser.FullName);
			Assert.AreEqual(newUser.LastName, loadedUser.LastName);
			Assert.AreEqual(newUser.UserName, loadedUser.UserName);



		}

	}
}

